package com.xxx.video.main.fragments;

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.xxx.video.R;
import com.xxx.video.commen.fragments.BaseFragment;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.views.recyclerviews.RefreshRecyclerView;
import com.xxx.video.main.adapters.FriendMessageListAdapter;
import com.xxx.video.main.beans.FriendMessageBean;
import com.xxx.video.main.presenters.FriendMessagePresenter;
import com.xxx.video.main.presenters.IFriendMessageView;

import java.util.Objects;

import butterknife.BindView;

public class FriendMessageFragment extends BaseFragment<FriendMessagePresenter> implements IFriendMessageView, RefreshRecyclerView.OnRecyclerViewRefreshOrLoadMoreListener {
    @BindView(R.id.rcv_friend_message_list)
    RefreshRecyclerView rcvFriendMessageList;
    /**
     *
     */
    private int page = 1;
    private FriendMessageListAdapter mFriendMessageListAdapter;

    public static FriendMessageFragment newInstance(Bundle args) {
        FriendMessageFragment fragment = new FriendMessageFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected FriendMessagePresenter createPresenter() {
        return new FriendMessagePresenter(Objects.requireNonNull(getActivity()), this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_friend_message;
    }

    @Override
    public void initData(Bundle extras) {
        initList();
        getPresenter().friendsPrivate(page);
    }

    @Override
    public void refreshData(Bundle extras) {
        getPresenter().friendsPrivate(page);
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }

    @Override
    public void initList() {
        rcvFriendMessageList.setLayoutManager(new LinearLayoutManager(getActivity()));
        mFriendMessageListAdapter = new FriendMessageListAdapter(Objects.requireNonNull(getActivity()));
        rcvFriendMessageList.setAdapter(mFriendMessageListAdapter);
        rcvFriendMessageList.setOnRecyclerViewRefreshOrLoadMoreListener(this);
    }

    @Override
    public void initData2View(FriendMessageBean successOb) {
        showRootLayoutStatus(1);
        rcvFriendMessageList.resetState();
        if (page == 1) {
            mFriendMessageListAdapter.setData(successOb.page.list);
            if (ListUtils.isEmpty(successOb.page.list)) {
                showRootLayoutStatus(2);
            }
        } else {
            mFriendMessageListAdapter.addData(successOb.page.list);
        }
    }

    @Override
    public void onRecyclerviewRefresh() {
        page = 1;
        getPresenter().friendsPrivate(page);
    }

    @Override
    public void onRecyclerviewLoadMore() {
        page++;
        getPresenter().friendsPrivate(page);
    }
}
